package com.ddpt.permission.dubbo;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.lh.center.common.constant.RedisPrefix;
import com.lh.center.common.exception.CommonException;
import com.lh.center.common.web.session.SessionOperation;
import com.lh.center.permission.api.dubbo.DubboSilenceLoginService;
import com.lh.center.permission.api.session.UserSessionUser;
import com.lh.center.user.api.dubbo.DubboUserService;
import com.lh.center.user.api.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@DubboService
public class DubboSilenceLoginServiceImpl implements DubboSilenceLoginService {

    @DubboReference
    private DubboUserService dubboUserService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public UserSessionUser userLogin(Long merchantId,Long userId, String userUnionId) {
        UserEntity user = null;
        if(userId != null){
            user = dubboUserService.getById(userId);
        }else if(StringUtils.isNotBlank(userUnionId)){
            user = dubboUserService.getByUnionId(merchantId,userUnionId);
        }

        if(user == null){
            throw new CommonException("登陆认证失败");
        }
        return loginSuccess(user);
    }

    private UserSessionUser loginSuccess(UserEntity user){
        UserSessionUser sessionUser = new UserSessionUser();
        //生成随机密码，并覆盖之前的
        user.setRandomSecret(UUID.randomUUID().toString().replace("-",""));
        String clientIP = ServletUtil.getClientIP(SessionOperation.getRequest());
        dubboUserService.updateLastRequest(user.getId(),user.getRandomSecret(),clientIP);
        // set 基础信息 start
        sessionUser.setGradleId(user.getGradleId());
        sessionUser.setUnionId(user.getUnionId());
        sessionUser.setAccount(user.getUnionId());
        sessionUser.setPhone(user.getPhone());
        sessionUser.setNickName(user.getNickName());
        sessionUser.setRealName(user.getRealName());
        sessionUser.setHeaderImg(user.getHeaderImg());
        sessionUser.setRandomSecret(user.getRandomSecret());
        sessionUser.setNewRegister(user.isNewRegister());
        // set 基础信息 end
        sessionUser.setUserId(user.getId());
        // notes 用户的机构id和瑞丰的机构id区分开 瑞丰用insIdStr字段。
        sessionUser.setInsId(user.getInsId());
        sessionUser.setMerchantId(user.getMerchantId());
        String token = UUID.randomUUID().toString().replace("-","");
        sessionUser.setToken(token);
        sessionUser.setSecret(RandomUtil.randomString(16));
        stringRedisTemplate.opsForValue().set(RedisPrefix.USER_ADMIN_USER_TOKEN+token, JSONUtil.toJsonStr(sessionUser),1, TimeUnit.DAYS);
        return sessionUser;
    }
}
